package com.qipay.core.entity;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qipay.core.exception.SystemException;
import com.qipay.core.utils.CommUtil;

import java.util.List;
import java.util.Objects;
import java.util.function.Supplier;

/**
 * @author 游永年 2017/8/27
 * @version V1.0.0
 * @description 分页实体类, 添加到controller收参对象里面
 * 注意:名称要与前端约定好,(pagination);
 * todo 前端没有传参数,默认是第一页第十条;
 * 用法:
 * pagination.startPage();
 * List<T> list=DaoHelper.select(param);//进行查询
 * pagination.endPage(list);
 */
public class Pagination {
    /**
     * 前端传过来的当前页数
     */
    private Integer page;
    private Integer pageSize;
    private Long total;
    private Integer pages;

    public Integer getPages() {
        return pages;
    }

    public void setPages(Integer pages) {
        this.pages = pages;
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }

    /**
     * 如果是null建一个init10页第一页的分页对象反回
     * 不是null直接返回
     *
     * @param pagination
     * @return
     */
    public static Pagination nullInit(Pagination pagination) {
        if (CommUtil.isEmpty(pagination) || CommUtil.isEmpty(pagination.getPage())) {
            pagination = new Pagination();
            pagination.setPage(1);
            pagination.setPageSize(10);
        }
        return pagination;
    }


    /**
     * 以当前页,和每以大小建一个pagination对象
     *
     * @return
     */
    public static Pagination instance(int p, int pz) {
        Pagination pagination = new Pagination();
        pagination.setPage(p);
        pagination.setPageSize(pz);
        return pagination;
    }

    /**
     * 调用此方法后的查询会分页
     */
    public static Pagination startPage(Pagination pagination) {
        if (CommUtil.isEmpty(pagination)) {
            throw new SystemException("分页器出错", "前端没有传分页器过来", "请联系管理员");
        }
        if (CommUtil.isEmpty(pagination.page)) {
            pagination.setPage(1);
        }
        if (CommUtil.isEmpty(pagination.pageSize)) {
            pagination.setPageSize(10);
        }
        PageHelper.startPage(pagination.getPage(), pagination.getPageSize());
        return pagination;
    }

    /**
     * 将查询后的list传入,得到Pagination对象返回前端
     *
     * @param list 查询后的list
     * @return
     */
    public Pagination endPage(List list) {
        if (CommUtil.isEmpty(list)) {
            this.total = 0l;
            this.pages = 1;
        }
        PageInfo page = new PageInfo(list);
        this.pageSize = page.getPageSize();
        this.total = page.getTotal();
        this.page = page.getPageNum();
        this.pages = page.getPages();
        return this;
    }

    /**
     * 新增java8的分页调用方法
     *
     * @param mapper
     * @param pagination
     * @param <R>
     * @return
     */
    public static <R> List<R> wrapper(Supplier mapper, Pagination pagination) {
        Objects.requireNonNull(mapper, "mapper不能为空");
        Objects.requireNonNull(pagination, "mapper不能为空");
        Pagination.startPage(pagination);
        List<R> result = (List<R>) mapper.get();
        pagination.endPage(result);
        return result;
    }
}
